package com.sas.web.articles;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.IValidationError;

import com.sas.domain.Article;
import com.sas.domain.Client;
import com.sas.utils.MessageHelper;
import com.sas.web.BasePage;

public class ConfirmAmountPage extends BasePage {
	private transient Integer amount;

	public ConfirmAmountPage(IModel<Client> clientModel, IModel<Article> articleModel,
			final ModalWindow modal1) {
		
		// count
		// button accept, cancel
		
		Form<Void> form = new Form<Void>("form", new CompoundPropertyModel<Void>(this));
		form.add(new Label("message", MessageHelper.sellingTo(articleModel, clientModel)));
		form.add(new TextField<Integer>("amount").setRequired(true));
		
		AjaxButton accept = new AjaxButton("accept", new ResourceModel("accept"), form) {
			
		
			/**
			 * 
			 */
			private static final long serialVersionUID = 2162031873992025876L;

			@Override
			protected void onError(AjaxRequestTarget target,
					Form<?> form) {
				// TODO Auto-generated method stub
				System.out.println("on error!!!!!!!!!");
				FeedbackPanel feedback = new FeedbackPanel("feedback");
				feedback.setOutputMarkupId(true);
				
				target.addComponent(feedback);
				super.onError(target, form);
			}

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// Update the feedback panel
				if (amount == null) {
					getForm().addOrReplace(new Label("message", "amout null"));
				} else {
					modal1.close(target);
					
				}

			}
		};
		
		
		
		final AjaxButton cancel = new AjaxButton("cancel", 
				new ResourceModel("cancel"), form) {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				modal1.close(target);
			}
		};
		
		form.add(accept);
		form.add(cancel);
		
		FeedbackPanel feedback = new FeedbackPanel("feedback");
		
		feedback.setOutputMarkupId(true);
		add(feedback);
		add(form);		
	}

}
